package cn.janus.service.impl;

import cn.janus.dao.persist.respository.IBillRepository;
import cn.janus.pojo.param.BillRecordParam;
import cn.janus.pojo.po.BillPO;
import cn.janus.pojo.vo.*;
import cn.janus.service.IBillService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillServiceImpl implements IBillService {
    private final IBillRepository billRepository;

    public BillServiceImpl(IBillRepository billRepository) {
        this.billRepository = billRepository;
    }

    @Override
    public void record(List<BillRecordParam> params) {
        List<BillRecordParam> temps = new ArrayList<>();
        for (BillRecordParam param : params) {
            if (param.getAmount() != null) {
                temps.add(param);
            }
        }
        List<BillPO> billPOS = changeParams(temps);
        billRepository.record(billPOS);
    }

    /**
     * 将入参集合转化为PO集合
     *
     * @param params 入参集合
     * @return PO集合
     */
    private List<BillPO> changeParams(List<BillRecordParam> params) {
        List<BillPO> billPOS = new ArrayList<>();
        for (BillRecordParam param : params) {
            BillPO billPO = new BillPO();
            BeanUtils.copyProperties(param, billPO);
            billPOS.add(billPO);
        }
        return billPOS;
    }

    @Override
    public List<BillVO> billDetail() {
        List<BillPO> billPOS = billRepository.billDetail();
        List<BillVO> list = new ArrayList<>();
        for (BillPO bill : billPOS) {
            BillVO billVO = new BillVO();
            BeanUtils.copyProperties(bill, billVO);
            list.add(billVO);
        }
        return list;
    }

    @Override
    public BillForMonthVO billForMonthService(String date) {
        YearMonth yearMonth = YearMonth.parse(date);
        LocalDate firstDay = yearMonth.atDay(1);
        LocalDate endDay = yearMonth.atEndOfMonth();
        List<BillPO> billPOS = billRepository.billForMonthRepository(firstDay, endDay);
        return new BillForMonthVO(billPOS);
    }

    @Override
    public List<BillTripVO> billTripService() {
        List<BillPO> list = billRepository.billTripRepository();
        List<BillTripVO> billTripVOS = new ArrayList<>();
        for (BillPO billPO : list) {
            boolean isNewTrip = true;
            for (BillTripVO billTripVO : billTripVOS) {
                if (billTripVO.getTripName().equals(billPO.getRemark())) {
                    BillVO billVO = new BillVO();
                    BeanUtils.copyProperties(billPO, billVO);
                    billTripVO.getBillVOS().add(billVO);
                    if (billPO.getCategory().equals("收入")) {
                        billTripVO.setTripFee(billTripVO.getTripFee().subtract(billPO.getAmount()));
                    } else {
                        billTripVO.setTripFee(billTripVO.getTripFee().add(billPO.getAmount()));
                    }
                    isNewTrip = false;
                }
            }
            if (isNewTrip) {
                BillTripVO billTripVO = new BillTripVO();
                billTripVO.setTripName(billPO.getRemark());
                billTripVO.setTripFee(billPO.getAmount());
                billTripVO.setBillVOS(new ArrayList<>());
                BillVO billVO = new BillVO();
                BeanUtils.copyProperties(billPO, billVO);
                billTripVO.getBillVOS().add(billVO);
                billTripVOS.add(billTripVO);
            }
        }
        return billTripVOS;
    }
}
